package com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel

import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.async
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.StuStudyAnalysisInfo
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.repo.StudyAnalysisRepo
import java.math.BigDecimal

/**
 * Created by Dadong on 2021/1/26
 * Describe:
 */
public class StuStudyAnalysisViewModel constructor(private var repo: StudyAnalysisRepo) : ViewModel() {

    var title: ObservableField<String> = ObservableField<String>("")
    var studyAnalysisInfo: ObservableField<StuStudyAnalysisInfo> = ObservableField<StuStudyAnalysisInfo>()
    var weekStudyAnalysisInfo = ObservableField<StuStudyAnalysisInfo>()
    var monthStudyAnalysisInfo = ObservableField<StuStudyAnalysisInfo>()
    var isWeek: ObservableField<Boolean> = ObservableField(true)
    var count = ObservableField<String>()
    var maxItem = ObservableField<String>()
    var minItem = ObservableField<String>()

    fun getStatisticData(): MutableList<String> {
        var statisticData = ArrayList<String>()
        statisticData.add(studyAnalysisInfo.get()!!.StuLearnAnalysis.PaperNum.toString())
        statisticData.add("${(studyAnalysisInfo.get()!!.StuLearnAnalysis.AvgScoreRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}")
        statisticData.add(studyAnalysisInfo.get()!!.StuLearnAnalysis.PaperScoreRateRank.toString())
        statisticData.add("${(studyAnalysisInfo.get()!!.StuLearnAnalysis.PersonalPaperSubRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}")
        statisticData.add("${(studyAnalysisInfo.get()!!.StuLearnAnalysis.PersonalExcellentRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}")
        statisticData.add("${(studyAnalysisInfo.get()!!.StuLearnAnalysis.PersonalStandardRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}")
        return statisticData
    }

    /*
  * 获取班级学情分析报告
  * */
    fun getClassLearningAnalysis(stuId: String) =
            repo.getStuLearnAnalysisApp(-1, stuId)
                    .async()
                    .doOnSuccess { it ->
                        studyAnalysisInfo.set(it.data)
                        count.set(it.data.StuPaperAnswerDetail.TotalPaperCount.toString())
                        var maxItemString = ""
                        it.data.StuQuesTypeAnalysis.MaxRateGenreName.forEachIndexed { index, s ->
                            if (index <= 1) {
                                maxItemString += "$s "
                            }
                        }
                        if (it.data.StuQuesTypeAnalysis.MaxRateGenreName.size > 2) {
                            maxItemString += "...等"
                        }

                        if (maxItemString.isEmpty()) {
                            maxItemString = "无"
                        }
                        maxItem.set(maxItemString)

                        var minItemString = ""
                        it.data.StuQuesTypeAnalysis.MinRateGenreName.forEachIndexed { index, s ->
                            if (index <= 1)
                                minItemString += "$s "
                        }
                        if (it.data.StuQuesTypeAnalysis.MinRateGenreName.size > 2) {
                            minItemString += "...等"
                        }

                        if (minItemString.isEmpty()) {
                            minItemString = "无"
                        }
                        minItem.set(minItemString)
                        title.set(it.data.StuLearnAnalysis.StuName + "的学情分析")
                    }

    /**
     * 获取一周学情分析报告
     */
    fun getWeekClassLearningAnalysis(stuId: String) =
            repo.getStuLearnAnalysisApp(7, stuId)
                    .async()
                    .doOnSuccess {
                        weekStudyAnalysisInfo.set(it.data)
                    }

    /**
     * 获取一个月的学情分析报告
     */
    fun getMonthClassLearningAnalysis(stuId: String) =
            repo.getStuLearnAnalysisApp(30, stuId)
                    .async()
                    .doOnSuccess {
                        monthStudyAnalysisInfo.set(it.data)
                    }
}